home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / SciAn / src / ScianScales.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  11KB  |  439 lines

  1. /*  ScianScales.c
  2.  *  scale objects for sliders and other controls
  3.  * 
  4.  *  Jim Lyons
  5.  *  2/3/93
  6.  *
  7.  *  "Rhapsody on a Theme of Pepke"
  8.  */
  9.  
  10.  
  11. #include "Scian.h"
  12. #include "ScianTypes.h"
  13. #include "ScianFontSystem.h"
  14. #include "ScianArrays.h"
  15. #include "ScianStyle.h"
  16. #include "ScianColors.h"
  17. #include "ScianIDs.h"
  18. #include "ScianHelp.h"
  19. #include "ScianErrors.h"
  20. #include "ScianControls.h"
  21. #include "ScianWindows.h"
  22. #include "ScianObjWindows.h"
  23. #include "ScianEvents.h"
  24. #include "ScianScripts.h"
  25. #include "ScianMethods.h"
  26. #include "ScianDraw.h"
  27. #include "ScianSliders.h"
  28. #include "ScianTextBoxes.h"
  29. #include "ScianSymbols.h"
  30. #include "ScianScales.h"
  31.  
  32. /* static method declarations */
  33. static ObjPtr DrawScale();
  34. static ObjPtr PressScale();
  35. static ObjPtr SetVal();
  36. static ObjPtr MakeHelpString();
  37.  
  38. ObjPtr scaleClass;
  39.  
  40. void InitScales()
  41. {
  42.     scaleClass = NewObject(controlClass, 0);
  43.     AddToReferenceList(scaleClass);
  44.     
  45.     SetVar(scaleClass, NAME, NewString("Scale"));
  46.     SetVar(scaleClass, UIFONT, NewInt(SCALEFONT));
  47.     SetVar(scaleClass, TEXTCOLOR, NewInt(SCALETEXTCOLOR));
  48.     SetVar(scaleClass, COLOR, NewInt(SCALELINECOLOR));
  49.     SetVar(scaleClass, LINEWIDTH, NewInt(SCALELINEWIDTH));
  50.     SetVar(scaleClass, FORMAT, NewString(SCALEFORMAT));
  51.     SetVar(scaleClass, BACKGROUND, NewInt(UIBACKGROUND));
  52.     SetVar(scaleClass, TYPESTRING, NewString("scale"));
  53.     
  54.     SetMethod(scaleClass, DRAW, DrawScale);
  55.     SetMethod(scaleClass, PRESS, PressScale);
  56.     SetMethod(scaleClass, SETVAL, SetVal);
  57.     SetMethod(scaleClass, MAKE1HELPSTRING, MakeHelpString);
  58. }
  59.  
  60. void KillScales()
  61. {
  62.     DeleteThing(scaleClass);
  63. }
  64.  
  65. #ifdef PROTO
  66. ObjPtr NewScale(int left, int right, int bottom, int top, int orientation,
  67.     Bool adj, char *name)
  68. #else
  69. ObjPtr NewScale(left, right, bottom, top, orientation, adj, name)
  70. int left, right, bottom, top, orientation;
  71. Bool adj;
  72. char *name;
  73. #endif
  74. {
  75.     ObjPtr newScale;
  76.     
  77.     newScale = NewObject(scaleClass, 0);
  78.     Set2DIntBounds(newScale, left, right, bottom, top);
  79.  
  80.     SetVar(newScale, NAME, NewString(name));
  81.     SetVar(newScale, HIGHLIGHTED, ObjFalse);
  82.     SetVar(newScale, ACTIVATED, adj ? ObjTrue : ObjFalse);
  83.     SetVar(newScale, ORIENTATION, NewInt(orientation));
  84.     switch (orientation)
  85.     {
  86.     case SO_TOP:
  87.     case SO_BOTTOM:
  88.         SetVar(newScale, ALIGNMENT, NewInt(CENTERALIGN));
  89.         SetVar(newScale, STEPPIXELS, NewInt(HSTEPPIXELS));
  90.         break;
  91.         
  92.     case SO_LEFT:
  93.         SetVar(newScale, ALIGNMENT, NewInt(RIGHTALIGN));
  94.         SetVar(newScale, STEPPIXELS, NewInt(VSTEPPIXELS));
  95.         break;
  96.         
  97.     case SO_RIGHT:
  98.         SetVar(newScale, ALIGNMENT, NewInt(LEFTALIGN));
  99.         SetVar(newScale, STEPPIXELS, NewInt(VSTEPPIXELS));
  100.         break;
  101.     }
  102.     return newScale;
  103. }
  104.  
  105. #ifdef PROTO
  106. void SetScaleRange(ObjPtr scale, real minVal, real maxVal)
  107. #else
  108. void SetScaleRange(scale, minVal, maxVal)
  109. ObjPtr scale;
  110. real minVal, maxVal;
  111. #endif
  112. {
  113.     ObjPtr valArray;
  114.     real *p;
  115.     int numDiv, left, right, bottom, top, length, orientation, stepPixels;
  116.     double majStep;
  117.     
  118.     Get2DIntBounds(scale, &left, &right, &bottom, &top);
  119.     orientation = GetInt(GetVar(scale, ORIENTATION));
  120.     stepPixels = GetInt(GetVar(scale, STEPPIXELS));
  121.  
  122.     if (orientation == SO_LEFT || orientation == SO_RIGHT) /* vertical scale */
  123.     {
  124.     length = top - bottom - 2*VSCALEINSET;
  125.     }
  126.     else /* horizontal scale */
  127.     {
  128.     length = right - left - 2*HSCALEINSET;
  129.     }
  130.     CalcGoodSteps((double) (maxVal - minVal), length, stepPixels, &majStep, &numDiv);
  131.     valArray = NewRealArray(1, 4L);
  132.     p = ELEMENTS(valArray);
  133.     p[0] = minVal;
  134.     p[1] = maxVal;
  135.     p[2] = majStep;
  136.     p[3] = (real) numDiv;
  137.     
  138.     SetVar(scale, VALUE, valArray);
  139.     ImInvalid(scale);
  140.     return;
  141. }
  142.  
  143. #ifdef PROTO
  144. void SetScaleFormat(ObjPtr scale, char *format)
  145. #else
  146. void SetScaleFormat(scale, format)
  147. ObjPtr scale;
  148. char *format;
  149. #endif
  150. {
  151.     SetVar(scale, FORMAT, NewString(format));
  152.     ImInvalid(scale);
  153.     return;
  154. }
  155.  
  156. #ifdef PROTO
  157. void SetScaleStepPixels(ObjPtr scale, int stepPixels)
  158. #else
  159. void SetScaleStepPixels(ObjPtr scale, int stepPixels)
  160. ObjPtr scale;
  161. int stepPixels;
  162. #endif
  163. {
  164.     ObjPtr valArray;
  165.  
  166.     SetVar(scale, STEPPIXELS, NewInt(stepPixels));
  167.     
  168.     if (valArray = GetVar(scale, VALUE)) /* recompute scale parameters */
  169.     {
  170.         real *p;
  171.  
  172.     p = ELEMENTS(valArray);
  173.     SetScaleRange(scale, p[0], p[1]);
  174.     }   
  175.     return;
  176.     
  177. }
  178.  
  179. #ifdef PROTO
  180. void LinkScale(ObjPtr scale, ObjPtr control)
  181. #else
  182. void LinkScale(scale, control)
  183. ObjPtr scale, control;
  184. #endif
  185. /* sets the REPOBJ of the scale to control and THESCALE of control to scale */
  186. {
  187.     SetVar(scale, REPOBJ, control);
  188.     SetVar(control, THESCALE, scale);
  189.     return;
  190. }
  191.  
  192. #ifdef PROTO
  193. static ObjPtr MakeHelpString(ObjPtr scale,  ObjPtr class)
  194. #else
  195. static ObjPtr MakeHelpString(scale)
  196. ObjPtr scale,  class;
  197. #endif
  198. {
  199.     if (IsTrue(GetVar(scale, ACTIVATED)))
  200.     {
  201.     /* scale is adjustable */
  202.     SetVar(class, HELPSTRING, NewString("\
  203. This is an adjustable scale. This means that it may be adjusted. Unfortunately, \
  204. there is no way to adjust it yet. So, technically, it is not really adjustable."));
  205.     }
  206.     else SetVar(class, HELPSTRING, NULLOBJ);
  207.     return scale;
  208. }
  209.  
  210. #ifdef PROTO
  211. static ObjPtr SetVal(ObjPtr scale, ObjPtr valArray)
  212. #else
  213. static ObjPtr SetVal(scale, valArray)
  214. ObjPtr scale;
  215. ObjPtr valArray;
  216. #endif
  217. {
  218.     if (IsRealArray(valArray) && RANK(valArray) == 1 && DIMS(valArray)[0] == 4)
  219.     SetVar(scale, VALUE, valArray);
  220.     else ReportError("Scale SETVAL", "Bad value array");
  221.     return scale;
  222. }
  223.  
  224. #ifdef PROTO
  225. static ObjPtr DrawScale(ObjPtr scale)
  226. #else
  227. static ObjPtr DrawScale(scale)
  228. ObjPtr scale;
  229. #endif
  230. {
  231. #ifdef GRAPHICS
  232.     int left, right, bottom, top, length, orientation;
  233.     char *format, *textfont;
  234.     int k, numDiv, hilite, alignment, uiFont;
  235.     int textsize, bgcolor, textcolor, linecolor, linewidth;
  236.     ObjPtr obj, valArray;
  237.     real *p;
  238.     double epsilon, curVal, minVal, maxVal, range, bigStep, lilStep;
  239.     int curPt;
  240.  
  241.     Get2DIntBounds(scale, &left, &right, &bottom, &top);
  242.     if (IsDrawingRestricted(left, right, bottom, top)) return NULLOBJ;
  243.     
  244.     right -= 1;
  245.     top -= 1;
  246.  
  247.     orientation = GetInt(GetVar(scale, ORIENTATION));
  248.     format = GetString(GetVar(scale, FORMAT));
  249.     hilite = GetInt(GetVar(scale, HIGHLIGHTED));
  250.     alignment = GetInt(GetVar(scale, ALIGNMENT));
  251.     if (obj = GetVar(scale, UIFONT))
  252.     {
  253.     uiFont = GetInt(obj);
  254.     SetUIFont(uiFont);
  255.     textsize = uiFontInfo[uiFont].size;
  256.     }
  257.     else
  258.     {
  259.     /* error if UIFONT was NULLed but font and size not set */
  260.     obj = GetStringVar("DrawScale", scale, TEXTFONT);
  261.         if (obj) textfont = GetString(obj);
  262.     else return ObjFalse;
  263.     obj = GetIntVar("DrawScale", scale, TEXTSIZE);
  264.     if (obj) textsize = GetInt(obj);
  265.     else return ObjFalse;
  266.     SetupFont(textfont, textsize);
  267.     }
  268.  
  269.     textcolor = GetInt(GetVar(scale, TEXTCOLOR));
  270.     linecolor = GetInt(GetVar(scale, COLOR));
  271.  
  272.     linewidth = GetInt(GetVar(scale, LINEWIDTH));
  273.     SetLineWidth(linewidth);
  274.  
  275.     valArray = GetVar(scale, VALUE);
  276.     p = ELEMENTS(valArray);
  277.     minVal = p[0];
  278.     maxVal = p[1];
  279.     bigStep = p[2];
  280.     numDiv = (int) p[3];
  281.     lilStep = bigStep/numDiv;
  282.     length = (orientation == SO_RIGHT || orientation == SO_LEFT) ? 
  283.         top - bottom - 2* VSCALEINSET : right - left - 2*HSCALEINSET;
  284.     range = maxVal - minVal;
  285.     
  286.     /* set curVal to the first big tic on or below the scale */
  287.     curVal = ((long) (minVal/bigStep))*bigStep;
  288.     while (curVal > minVal) curVal -= bigStep;
  289.     
  290.     /* now find the first tic on the scale */
  291.     k = 0;
  292.     while (curVal < minVal)
  293.     {
  294.     ++k;
  295.     if (k >= numDiv) k = 0;
  296.     curVal += lilStep;
  297.     }
  298.     epsilon = range*1.0E-6;
  299.     if (ABS(curVal) < epsilon) curVal = 0.0;
  300.     
  301.     /* draw scale according to orientation */
  302.     switch (orientation)
  303.     {
  304.     case SO_LEFT:
  305.         DrawUILine(right, bottom + VSCALEINSET, right, top - VSCALEINSET, linecolor);
  306.         while (curVal <= maxVal + epsilon)
  307.         {
  308.         curPt = bottom + VSCALEINSET + (curVal - minVal)*length/range + .5;
  309.         if (k == 0)
  310.         {
  311.             /* big tic and number */
  312.             DrawUILine(right, curPt, right - BIGTIC, curPt, linecolor);
  313.             sprintf(tempStr, format, curVal);
  314.             DrawAString(alignment, right - BIGTIC - 2, curPt - textsize/2, tempStr); 
  315.         }
  316.         else
  317.         {
  318.             /* lil tic */
  319.             DrawUILine(right, curPt, right - LILTIC, curPt, linecolor);
  320.         }
  321.         curVal += lilStep;
  322.         if (ABS(curVal) < epsilon) curVal = 0.0;
  323.         ++k;
  324.         if (k >= numDiv) k = 0;
  325.         }
  326.         break;
  327.         
  328.     case SO_RIGHT:
  329.         DrawUILine(left, bottom + VSCALEINSET, left, top - VSCALEINSET, linecolor);
  330.         while (curVal <= maxVal + epsilon)
  331.         {
  332.         curPt = bottom + VSCALEINSET + (curVal - minVal)*length/range + .5;
  333.         if (k == 0)
  334.         {
  335.             /* big tic and number */
  336.             DrawUILine(left, curPt, left + BIGTIC, curPt, linecolor);
  337.             sprintf(tempStr, format, curVal);
  338.             DrawAString(alignment, left + BIGTIC + 2, curPt - textsize/2, tempStr); 
  339.         }
  340.         else
  341.         {
  342.             /* lil tic */
  343.             DrawUILine(left, curPt, left + LILTIC, curPt, linecolor);
  344.         }
  345.         curVal += lilStep;
  346.         if (ABS(curVal) < epsilon) curVal = 0.0;
  347.         ++k;
  348.         if (k >= numDiv) k = 0;
  349.         }
  350.         break;
  351.         
  352.     case SO_BOTTOM:
  353.         DrawUILine(left + HSCALEINSET, top, right - HSCALEINSET, top, linecolor);
  354.         while (curVal <= maxVal + epsilon)
  355.         {
  356.         curPt = left + HSCALEINSET + (curVal - minVal)*length/range + .5;
  357.         if (k == 0)
  358.         {
  359.             /* big tic and number */
  360.             DrawUILine(curPt, top, curPt, top - BIGTIC, linecolor);
  361.             sprintf(tempStr, format, curVal);
  362.             DrawAString(alignment, curPt, top - textsize - BIGTIC - 2, tempStr); 
  363.         }
  364.         else
  365.         {
  366.             /* lil tic */
  367.             DrawUILine(curPt, top, curPt, top - LILTIC, linecolor);
  368.         }
  369.         curVal += lilStep;
  370.         if (ABS(curVal) < epsilon) curVal = 0.0;
  371.         ++k;
  372.         if (k >= numDiv) k = 0;
  373.         }
  374.         break;
  375.         
  376.     case SO_TOP:
  377.         DrawUILine(left + HSCALEINSET, bottom, right - HSCALEINSET, bottom, linecolor);
  378.         while (curVal <= maxVal + epsilon)
  379.         {
  380.         curPt = left + HSCALEINSET + (curVal - minVal)*length/range + .5;
  381.         if (k == 0)
  382.         {
  383.             /* big tic and number */
  384.             DrawUILine(curPt, bottom, curPt, bottom + BIGTIC, linecolor);
  385.             sprintf(tempStr, format, curVal);
  386.             DrawAString(alignment, curPt, bottom + BIGTIC + 2, tempStr); 
  387.         }
  388.         else
  389.         {
  390.             /* lil tic */
  391.             DrawUILine(curPt, bottom, curPt, bottom + LILTIC, linecolor);
  392.         }
  393.         curVal += lilStep;
  394.         if (ABS(curVal) < epsilon) curVal = 0.0;
  395.         ++k;
  396.         if (k >= numDiv) k = 0;
  397.         }
  398.         break;
  399.     }
  400.     SetLineWidth(1);
  401. #endif
  402.     return ObjTrue;
  403. }
  404.  
  405. #ifdef PROTO
  406. static ObjPtr PressScale(ObjPtr scale, int mouseX, int mouseY, int flags)
  407. #else
  408. static ObjPtr PressScale(scale, mouseX, mouseY, flags)
  409. ObjPtr scale;
  410. int mouseX, mouseY;
  411. long flags;
  412. #endif
  413. {
  414. #ifdef INTERACTIVE
  415.     int left, right, bottom, top;
  416.  
  417.     Get2DIntBounds(scale, &left, &right, &bottom, &top);
  418.  
  419.     /* return if mouse outside slider rectangle */
  420.     if (mouseX < left - 1 || mouseX > right + 1 || mouseY < bottom -1
  421.             || mouseY > top + 1) return ObjFalse;
  422.             
  423.     if (TOOL(flags) == T_HELP)
  424.     {
  425.     ContextHelp(scale);
  426.     ContextHelp(GetVar(scale, REPOBJ));
  427.     return ObjTrue;
  428.     }
  429.  
  430.     /* return if not active */
  431.     if (!GetPredicate(scale, ACTIVATED)) return ObjTrue;
  432.     
  433.     /*** adjust the scale! ***/
  434.  
  435. #endif    
  436.     return ObjTrue;
  437. }
  438.  
  439.